﻿/*
Copyright (c) 2011 <a href="http://www.gutgames.com">James Craig</a>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/

#region Usings
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using Utilities.DataTypes.Patterns.BaseClasses;
using YABOV.Attributes.BaseClasses;
using YABOV.DataTypes;
using YABOV.Exceptions;
#endregion

namespace YABOV
{
    /// <summary>
    /// Validation manager class
    /// </summary>
    public class ValidationManager : Singleton<ValidationManager>
    {
        #region Constructor

        /// <summary>
        /// Constructor
        /// </summary>
        protected ValidationManager()
            : base()
        {
            Properties = new Dictionary<Type, DataClass>();
        }

        #endregion

        #region Public Functions

        /// <summary>
        /// Validates an object
        /// </summary>
        /// <param name="Object">object to validate</param>
        /// <returns>true if it is valid, otherwise an exception is thrown</returns>
        public static bool Validate(object Object)
        {
            StringBuilder Builder = new StringBuilder();
            if (Object != null)
            {
                Type ObjectType = Object.GetType();
                Type[] Interfaces = ObjectType.GetInterfaces();
                PropertyInfo[] Properties = ObjectType.GetProperties();
                foreach (PropertyInfo Property in Properties)
                {
                    object[] Attributes = Property.GetCustomAttributes(typeof(BaseAttribute), true);
                    foreach (object Attribute in Attributes)
                    {
                        Builder.Append(((BaseAttribute)Attribute).IsValid(Property, Object));
                    }
                    foreach (Type Interface in Interfaces)
                    {
                        try
                        {
                            PropertyInfo TempProperty = Interface.GetProperty(Property.Name);
                            if (TempProperty != null)
                            {
                                object[] InterfaceAttributes = TempProperty.GetCustomAttributes(typeof(BaseAttribute), true);
                                foreach (object Attribute in InterfaceAttributes)
                                {
                                    Builder.Append(((BaseAttribute)Attribute).IsValid(Property, Object));
                                }
                            }
                        }
                        catch { }
                    }
                }
            }
            if (string.IsNullOrEmpty(Builder.ToString()))
                return true;
            throw new NotValid(Builder.ToString());
        }

        /// <summary>
        /// Validates an object
        /// </summary>
        /// <param name="Object">object to validate</param>
        /// <returns>true if it is valid, otherwise an exception is thrown</returns>
        public bool FasterValidate(object Object)
        {
            StringBuilder Builder = new StringBuilder();
            if (Object != null)
            {
                Type ObjectType = Object.GetType();
                if (!Properties.ContainsKey(ObjectType))
                {
                    Type[] Interfaces = ObjectType.GetInterfaces();
                    PropertyInfo[] TempProperties = ObjectType.GetProperties();
                    DataClass TempClass = new DataClass();
                    foreach (PropertyInfo Property in TempProperties)
                    {
                        object[] Attributes = Property.GetCustomAttributes(typeof(BaseAttribute), true);
                        foreach (object Attribute in Attributes)
                        {
                            TempClass.Attributes.Add(Property, (BaseAttribute)Attribute);
                        }

                        foreach (Type Interface in Interfaces)
                        {
                            try
                            {
                                PropertyInfo TempProperty = Interface.GetProperty(Property.Name);
                                if (TempProperty != null)
                                {
                                    object[] InterfaceAttributes = TempProperty.GetCustomAttributes(typeof(BaseAttribute), true);
                                    foreach (object Attribute in InterfaceAttributes)
                                    {
                                        TempClass.Attributes.Add(Property, (BaseAttribute)Attribute);
                                    }
                                }
                            }
                            catch { }
                        }
                    }
                    Properties.Add(ObjectType, TempClass);
                }
                foreach (PropertyInfo Key in Properties[ObjectType].Attributes.Keys)
                {
                    for (int x = 0; x < Properties[ObjectType].Attributes[Key].Count; ++x)
                    {
                        Builder.Append(Properties[ObjectType].Attributes[Key][x].IsValid(Key, Object));
                    }
                }
            }
            if (string.IsNullOrEmpty(Builder.ToString()))
                return true;
            throw new NotValid(Builder.ToString());
        }

        #endregion

        #region Properties

        internal Dictionary<Type, DataClass> Properties { get; set; }

        #endregion
    }
}